<?php
/*
	The special controller is specific to each implementation of push.
	For example there may be a function for setting an assessment
	as complete prematurely that only one institute needs.
*/
class SpecialController extends AppController 
{
	var $uses = array('Asession', 'Asessionlog', 'Asessionresult', 'Course', 'User', 'Assessment', 'Questiongroup', 'Question', 'Answer');
	var $helpers = array('Xml');
	
	function beforeFilter() {
	
		$this->layout = 'none';

		$this->Auth->allow('check');
		$this->Auth->allow('clear');
		$this->Auth->allow('get_assessment_list');
		$this->Auth->allow('get_questiongroup_list');
		$this->Auth->allow('get_answer_list');
		$this->Auth->allow('get_question_list');
		$this->Auth->allow('get_answers');
		$this->Auth->allow('get_answers_all');
		$this->Auth->allow('get_courses');
		$this->Auth->allow('get_available_assessments');
		$this->Auth->allow('get_qg_data');
		$this->Auth->allow('get_questionanswer_data');
		$this->Auth->allow('record_message');
		$this->Auth->allow('complete_assessment');
		$this->Auth->allow('get_prescription');
		$this->Auth->allow('get_question_ids');
		$this->Auth->allow('get_practice');
		$this->Auth->allow('get_completed_courses');
		$this->Auth->allow('get_complete_date');
		$this->Auth->allow('mobile_data');
		$this->Auth->allow('create_report');
		$this->Auth->allow('assign_startpoint');
		$this->Auth->allow('free_assessment');
		$this->Auth->allow('free_all');
	}
	
	function check($token, $question_id){

		if ($this->_validate_token($token)){

			$this->set('token', $token);

			$this->header('Content-Type: text/xml');

			$question = $this->Question->findById($question_id);
			$possibleAnswers = $this->Answer->findByquestion_id($question_id);
			print_r($possibleAnswers);
			
			$answer = $this->Answer->findById($answer_id);

			if (!empty($answer)){

				$asessionlog = $this->Asessionlog->find('first',
					array(
						'conditions' => array(
							'Asessionlog.asession_id' => $this->asession_id, 
							'Asessionlog.answer' => $answer_id,
							'Asessionlog.correct' => 1
						),
						'recursive' => 2
					)
				);

				if (!empty($asessionlog)){

					$this->set('token', $token);
					$this->set('status', 'true');
					$this->set('actionName', $question['Question']['action']);
					$this->set('asessionlog', $asessionlog);

				}else{

					$this->set('token', $token);
					$this->set('status', 'false');
					$this->set('actionName', $question['Question']['action']);
					$this->set('asessionlog', array());

				}
					
			}

		}else{

			$this->redirect('/asessions/index');

		}

	}
	function clear($token){
		
		// ensure that the method is GET only for REST compliance
		if (!$this->RequestHandler->isGet()){
			#error_log('/asessions/questiongroup must be of method GET');
			$this->set('message', 'INVALID HTTP REQUEST METHOD: expected GET');
			$this->header('HTTP/1.1 400 Bad Request');
			$this->render('index');
			return;
		}

		if ($this->_validate_token($token)){

			$asession = $this->Asession->findById($this->asession_id);

			foreach ($asession['Asessionlog'] as $asessionlog){
				$this->Asessionlog->delete($asessionlog['id']);
				$asessionresults = $this->Asessionresult->findById($asessionlog['id']);
				foreach ($asessionlog['Asessionresult'] as $asessionresult){
					$this->Asessionresult->delete($asessionresult['id']);
				}
			}

			$this->set('message',$token.' results cleared');

		}else{

			$this->redirect('/asessions/index');

		}

		$this->header('Content-Type: text/xml');

	}
	
	function _validate_token($token)
	{
		$asession_rec = $this->Asession->find('first',
			array(
				'conditions' => array('Asession.uniqueid' => $token),
			)
		);

		if (!empty($asession_rec))
		{
			$this->asession_id = $asession_rec['Asession']['id'];
			return true;
		}
		else
		{
			return false;
		}

	}

	function get_questiongroup_list($assessment_id = null, $pass = "")
	{
		if($pass != Configure::read('Settings.data_pass_code') || $assessment_id == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$questionGroups = $this->Questiongroup->find('all', 
														array
														(
															'conditions' => array('Assessment.id' => $assessment_id),
															'order' => array('Questiongroup.rank ASC'),
														));
			$qgroupIds = array();//list of question group ids
			$qgroupNames = "";
			$i = 0;
			foreach($questionGroups as $row)
			{
				if($row['Questiongroup']['message_only'] == 0)
				{
					$qgroupIds[$i] = array('qgid' => $row['Questiongroup']['id']);
					$qgroupNames .= $row['Questiongroup']['name'].":-:";
					$i ++;
				}
			}
			//trim the end of the qgroupNames;
			$qgroupNames = substr($qgroupNames, 0, -3);
			$this->set('data', gzcompress($qgroupNames, 8));
			//$this->set('data', $qgroupNames);
		}
	}
	
	function get_answer_list($assessment_id = null, $pass = "")
	{
		if($pass != Configure::read('Settings.data_pass_code') || $assessment_id == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$questionGroups = $this->Questiongroup->findAllByassessment_id($assessment_id);
			$qgroupIds = array();//list of question group ids
			$qgroupNames = "";
			$i = 0;
			foreach($questionGroups as $row)
			{
				if($row['Questiongroup']['message_only'] == 0)
				{
					$qgroupIds[$i] = array('qgid' => $row['Questiongroup']['id']);
					$qgroupNames .= $row['Questiongroup']['name'].":-:";
					$i++;
				}
			}
			//trim the end of the qgroupNames;
			$qgroupNames = substr($qgroupNames, 0, -3);
			
			//read through questions related to quesitongroup ids
			$questionIds = array();
			$questionNames = "";
			for($q = 0; $q < $i; ++$q)
			{
				$condition = $qgroupIds[$q]['qgid'];
				$questions = $this->Question->findAllByquestiongroup_id($condition);
				foreach($questions as $row)
				{
					$questionIds[$q] = array('qid' => $row['Question']['id']);
					$questionNames .= $row['Question']['name'] . ":-:";
				}
			}
			$questionNames = substr($questionNames, 0, -3);
			$questionNameData = explode(":-:", $questionNames);
			
			$numQuestions = count($questionIds);
			//read through answers
			$answerData = "";
			for($i = 0; $i < $numQuestions; ++$i)
			{
				$condition = $questionIds[$i]['qid'];
				$answers = $this->Answer->find('all', 
												array
												(
													'conditions' => array('Question.id' => $condition),
													'order' => array('Answer.rank ASC'),
												));
				$ansIndex = 0;
				if($i != 0)
					$answerData .= "$#$";
					
				foreach($answers as $row)
				{
					foreach($answers as $row2)
					{
						if($row2['Answer']['id'] == $row['Answer']['id'])
							continue;
						if($row2['Answer']['return_value'] == $row['Answer']['return_value'])
						{
							$answerData .= $row2['Answer']['id'] . "***";
						}
					}
					$answerData .= $row['Answer']['id'] . "^^" . $row['Answer']['return_value'].":-:";
					$ansIndex++;
				}
				$answerData = substr($answerData, 0, -3); 
			}
			
			$this->set('data', gzcompress($answerData, 8));
		}
	}
	
	function get_assessment_list($pass = "")
	{
		if($pass != Configure::read('Settings.data_pass_code'))
		{
			$this->set('data', "Error");
		}
		else
		{
			$assessments = $this->Assessment->find('all');
			$assessmentData = "";
			foreach($assessments as $assessment)
			{
			
				$courses = $this->Course->query("SELECT * FROM assessments_courses WHERE assessment_id='".$assessment['Assessment']['id']."' LIMIT 1");
				$course_id = $courses[0]['assessments_courses']['course_id'];
				$course = $this->Course->findById($course_id);
				$course_name = $course['Course']['name'];
				$assessmentData .= $assessment['Assessment']['name'] . "###" . $assessment['Assessment']['id'] . "###" . $assessment['Assessment']['learning_objective'] . "###" . $assessment['Assessment']['code'] . "###" . $assessment['Assessment']['modified'] . "###" . $course_name;
				$assessmentData .= ":-:";
			}
			
			$assessmentData = substr($assessmentData, 0, -3);
			$this->set('data', gzcompress($assessmentData, 8));
		}
	}
	
	function get_question_list($assessment_id = null, $pass = "")
	{
		if($pass != Configure::read('Settings.data_pass_code') || $assessment_id == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$questionGroups = $this->Questiongroup->findAllByassessment_id($assessment_id);
			$qgroupIds = array();//list of question group ids
			$qgroupNames = "";
			$i = 0;
			foreach($questionGroups as $row)
			{
				if($row['Questiongroup']['message_only'] == 0)
				{
					$qgroupIds[$i] = array('qgid' => $row['Questiongroup']['id']);
					$qgroupNames .= $row['Questiongroup']['name'].":-:";
					$i++;
				}
			}
			//trim the end of the qgroupNames;
			$qgroupNames = substr($qgroupNames, 0, -3);
			
			//read through questions related to quesitongroup ids
			$questionIds = array();
			$questionNames = "";
			for($q = 0; $q < $i; ++$q)
			{
				$condition = $qgroupIds[$q]['qgid'];
				$questions = $this->Question->find('all', 
													array
													(
														'conditions' => array('Questiongroup.id' => $condition),
														'order' => array('Question.rank ASC'),
													));
				foreach($questions as $row)
				{
					$questionIds[$q] = array('qid' => $row['Question']['id']);
					$questionNames .= $row['Question']['name'] . ":-:";
				}
			}
			$questionNames = substr($questionNames, 0, -3);
			
			$this->set('data', gzcompress($questionNames, 8));
		}
	}
	
	function get_answers($assessment_id = null, $answer_ids = null, $pass = "")
	{
		if($pass != Configure::read('Settings.data_pass_code') || $assessment_id == null || $answer_ids == null)
		{
			$this->set('data', "Error");
		}
		else
		{

			$answerIDs = explode(",", $answer_ids);
			$questionGroups = $this->Questiongroup->findAllByassessment_id($assessment_id);
			
			$qgroupIds = array();//list of question group ids
			$i = 0;
			foreach($questionGroups as $row)
			{
				if($row['Questiongroup']['message_only'] == 0)
				{
					$qgroupIds[$i] = array('qgid' => $row['Questiongroup']['id']);
					$i++;
				}
			}
			//read through questions related to quesitongroup ids
			$questionIds = array();
			for($q = 0; $q < $i; ++$q)
			{
				$condition = $qgroupIds[$q]['qgid'];
				$questions = $this->Question->findAllByquestiongroup_id($condition);
				foreach($questions as $row)
				{
					$questionIds[$q] = array('qid' => $row['Question']['id']);
				}
			}
			
			$numQuestions = count($questionIds);
			//read through answers
			$answerData = "";
			for($i = 0; $i < $numQuestions; ++$i)
			{
				$condition = $questionIds[$i]['qid'];
				$answers = $this->Answer->find('all', 
												array
												(
													'conditions' => array('Question.id' => $condition),
													'order' => array('Answer.rank ASC'),
												));
				$ansIndex = 0;
				$explode = explode("***", $answerIDs[$i]);
				if(count($explode) > 1)
				{
					$str = "";
					for($j = 0; $j < count($explode); ++$j)
					{
						$data = intval($explode[$j]);
						$answer = $this->Answer->findById($data);
						$str .= $answer['Answer']['answer_text']."***";
					}
					$str = substr($str, 0, -3);
					$answerData .= $str . ":-:";
				}
				foreach($answers as $row)
				{
					if($row['Answer']['id'] == $answerIDs[$i])
					{
						$answerData .= $row['Answer']['answer_text'].":-:";
					}
					++$ansIndex;
				}
			}
			$answerData = substr($answerData, 0, -3); 
			
			$this->set('data', gzcompress($answerData, 8));
		}
	}
	function get_courses($student_number = null, $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code') || $student_number == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$courses = $this->Course->find('all');
			$courseData = "";
			foreach($courses as $course)
			{
				$courseData .= $course['Course']['id'] . ":-:";
				$courseData .= $course['Course']['name'] . ":-:";
				$asessions = $this->Asession->find('first', 
					array
					(
						'conditions' => array
						(
							'Asession.student_number' => $student_number,
							'Asession.course_id' => $course['Course']['id'],
							'Asession.complete' => 1,
							'Asession.result' => 1,
						)
					)
				);
				if($asessions == 0)
					$courseData .= 0 . "$$$";
				else
					$courseData .= 1 . "$$$";
			}
			
			$courseData = substr($courseData, 0, -3); 
			
			$this->set('data', gzcompress($courseData, 8));
		}
	}
	function get_practice($course_id = null, $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code') || $course_id == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$assessments = $this->Assessment->find('all', 
				array
				(
					'conditions' => array
					(
						'Assessment.practice' => 1,
					)
				)
			);
			foreach($assessments as $assessment)
			{
				if($assessment['Course'][0]['id'] == $course_id)
				{
					$this->set('data', $assessment['Assessment']['id']);
					break;
				}
			}
		}
	}
	function get_available_assessments($student_number = null, $course_id = null, $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code') || $student_number == null || $course_id == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$student_number .= " Monash";
			$asessions = $this->Asession->find('all', 
				array
				(
					'conditions' => array
					(
						'Asession.course_id' => $course_id, 
						'Asession.student_number' => $student_number,
						'Asession.complete' => 0,
					)
				)
			);
			if(count($asessions) == 0)
			{
				$assessments = $this->Assessment->find('all');
				$assessmentList = "";
				foreach($assessments as $assessment)
				{
					if($assessment['Course'][0]['id'] == $course_id && $assessment['Assessment']['practice'] == 0)
					{
						$assessmentList .= $assessment['Assessment']['id'] . ":-:";
					}
				}
				
				$assessmentList = substr($assessmentList, 0, -3);
				$this->set('data', gzcompress($assessmentList, 8));
			}
			else
			{
				$assessments = $this->Assessment->find('all');
				$assessmentList = "";
				foreach($assessments as $assessment)
				{
					if($assessment['Course'][0]['id'] == $course_id)
					{
						$found = false;
						
						foreach($asessions as $asession)
						{
							if($asession['Asession']['assessment_id'] == $assessment['Assessment']['id'])
							{
								$found = true;
								break;
							}
							
						}
						if($found == false)
							$assessmentList .= $assessment['Assessment']['id'] . ":-:";
					}
				}
				
				$assessmentList = substr($assessmentList, 0, -3);
				$this->set('data', gzcompress($assessmentList, 8));
			}
		}
	}
	function get_qg_data($assessment_id = null, $pass = null)
	{
		$questionGroups = $this->Questiongroup->find('all', 
														array
														(
															'conditions' => array('Assessment.id' => $assessment_id),
															'order' => array('Questiongroup.rank ASC'),
														));
		$qgroupData = "";
		$i = 0;
		foreach($questionGroups as $row)
		{
			if($row['Questiongroup']['message_only'] == 0)
			{
				$qgroupData .= $row['Questiongroup']['name']."&&&".$row['Questiongroup']['id']."&&&".$row['Questiongroup']['longitude']."&&&".$row['Questiongroup']['latitude']."&&&".$row['Questiongroup']['description'].":-:";
				$i ++;
			}
		}
		//trim the end of the qgroupData;
		$qgroupData = substr($qgroupData, 0, -3);
		$this->set('data', gzcompress($qgroupData, 8));
	}
	function get_questionanswer_data($question_id, $pass)
	{
		//read through answers
		$data = "";
		$question = $this->Question->findByid($question_id);
		$data .= $question['Question']['id']."&&&".$question['Question']['question'].":-:";
		$answers = $this->Answer->findAllByquestion_id($question_id);
		$correct_id = 0;
		foreach($answers as $row)
		{
			$data .= $row['Answer']['id'].":::".$row['Answer']['answer_text']."***";
			if($row['Answer']['correct'] == 1)
				$correct_id = $row['Answer']['id'];
		}
		$data = substr($data, 0, -3); 
		$data .= ":-:" . $correct_id;
		
		$this->set('data', gzcompress($data, 8));
	}
	function record_message($token, $message)
	{
		$asession = $this->Asession->find('first',
			array
			(
				'conditions' => array('Asession.uniqueid' => $token),
			)
		);
		
		$this->Asessionlog->create();
		$this->Asessionlog->set(array('asession_id' => $asession['Asession']['id'], 'message' => $message));
		$this->Asessionlog->save();
	}
	
	function complete_assessment($token)
	{
		$asession = $this->Asession->find('first',
			array
			(
				'conditions' => array('Asession.uniqueid' => $token),
			)
		);
		
		$this->Asession->id = $asession['Asession']['id'];
		$this->Asession->set(array('complete' => 1, 'pass' => $pass));
		$this->Asession->save();
	}
	
	function get_question_ids($questiongroup_id, $pass = "")
	{
		$data = "";
		$questions = $this->Question->find('all', 
											array
											(
												'conditions' => array('Questiongroup.id' => $questiongroup_id),
												'order' => array('Question.rank ASC'),
											));
		foreach($questions as $question)
		{
			$data .= $question['Question']['id'] . "&&&";
		}
		$data = substr($data, 0, -3); 
		
		$this->set('data', gzcompress($data, 8));
	}
	
	function get_prescription($assessment_id, $pass = "")
	{
		$data = "";
		$questionGroup = $this->Questiongroup->find('first', 
												array
												(
													'conditions' => array
													(
														'Questiongroup.assessment_id' => $assessment_id,
														'Questiongroup.name' => "Labeling"
														
													)
												)
											);

		$questions = $this->Question->findAllByquestiongroup_id($questionGroup['Questiongroup']['id']);
		foreach($questions as $question)
		{
			$answers = $this->Answer->findAllByquestion_id($question['Question']['id']);
			foreach($answers as $answer)
			{
				if($answer['Answer']['correct'] == 1)
				{
					$data .= $question['Question']['name'] . "&&&" . $answer['Answer']['answer_text'] . ":-:";
				}
			}
		}
		$data = substr($data, 0, -3); 
		
		$this->set('data', gzcompress($data, 8));
	}
	function get_completed_courses($student_number = null, $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code') || $student_number == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$asessions = $this->Asession->find('all', 
				array
				(
					'conditions' => array
					(
						'Asession.student_number' => $student_number,
						'Asession.complete' => 1,
					)
				)
			);
			$data = "";
			foreach($asessions as $asession)
			{
				$data .= $asession['Asession']['course_id'] . ":-:";
			}
			$data = substr($data, 0, -3); 
			
			if(strlen($data) == 0)
				$this->set('data', "empty");
			else
				$this->set('data', $data);
		}
	}

	function get_complete_date($student_number = null, $course_id = null, $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code') || $student_number == null || $course_id == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$asession = $this->Asession->find('first', 
				array
				(
					'conditions' => array
					(
						'Asession.course_id' => $course_id, 
						'Asession.student_number' => $student_number,
						'Asession.complete' => 1,
						'Asession.result' => 1,
					)
				)
			);
			
			$this->set('data', $asession['Asession']['modified']);
		}
	}
	
	function create_report($a_id = null, $student_number = null, $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code') || $student_number == null || $a_id == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$asession = $this->Asession->find('first',
				array
				(
					'conditions' => array
					(
						'Asession.student_number' => $student_number,
						'Asession.assessment_id' => $a_id,
					)
				)
			);
			
			$numAsessions = $this->Asession->find('count',
				array
				(
					'conditions' => array
					(
						'Asession.student_number' => $student_number,
						'Asession.assessment_id' => $a_id,
					)
				)
			);
			
			if($numAsessions == 0)
			{
				$this->set('data', "Error");
				return;
			}
			$asessions = $this->Asession->find('all',
				array
				(
					'conditions' => array
					(
						'Asession.student_number' => $student_number,
						'Asession.assessment_id' => $a_id,
					)
				)
			);
			$i = 0;
			$aid = 0;
			foreach($asessions as $asession)
			{
				if($i == $numAsessions)
					$aid = $asession['Asession']['id'];
			}
			
			
			$aid = $asession['Asession']['id'];
			$course_id = $asession['Asession']['course_id'];
			
			$asessionLogs = $this->Asessionlog->find('all', 
				array
				(
					'conditions' => array
					(
						'Asessionlog.asession_id' => $aid,
					)
				)
			);
			$previousQuestionID = -1;
			$question = "";
			$data = $course_id . "+++";
			foreach($asessionLogs as $result)
			{
				if($result['Asessionlog']['questiongroup_id'] > 0)
				{
					$qID = $result['Asessionlog']['questiongroup_id'];
					$questiongroup = $this->Questiongroup->findById($qID);
					//messages aren't needed
					if($questiongroup['Questiongroup']['message_only'] == 1)
						continue;
					$data .= "g---".$questiongroup['Questiongroup']['name'].":-:";
				}
				else if($result['Asessionlog']['question_id'] > 0)
				{
					$qID = $result['Asessionlog']['question_id'];
					//prevent duplicates
					//if($qID == $previousQuestionID)
					//	continue;
					$questionData = $this->Question->findById($qID);
					$question = $questionData['Question']['question'];
					//$previousQuestionID = $qID;
					//$data .= "q---".$question['Question']['question']."---".$result['Asessionlog']['correct'].":-:";
				}
				else if($result['Asessionlog']['answer'] > 0)
				{
					$data .= "q---".$question."---".$result['Asessionlog']['correct'].":-:";
				}
				else if($result['Asessionlog']['message'] != "")
				{
					$data .= "m---".$result['Asessionlog']['message'] . ":-:";
				}
			}
			$data = substr($data, 0, -3);
			
			$this->set('data', gzcompress($data, 8));
			//$this->set('data', $data);
		}
	}
	function mobile_data($aid = null, $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code') || $assessment_id = null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$questionGroups = $this->Questiongroup->find('all', 
														array
														(
															'conditions' => array('Assessment.id' => $aid),
															'order' => array('Questiongroup.rank ASC'),
														));
			$data = "";
			$i = 0;
			foreach($questionGroups as $row)
			{
				if($row['Questiongroup']['message_only'] == 0)
				{
					$data .= $row['Questiongroup']['name']."&&&".$row['Questiongroup']['id']."&&&".$row['Questiongroup']['longitude']."&&&".$row['Questiongroup']['latitude']."&&&".$row['Questiongroup']['description']."&&&";
					$questiongroup_id = $row['Questiongroup']['id'];
					
					$questions = $this->Question->find('all', 
											array
											(
												'conditions' => array('Questiongroup.id' => $questiongroup_id),
												'order' => array('Question.rank ASC'),
											));
					foreach($questions as $question)
					{
						$question_id = $question['Question']['id'];
						$question = $this->Question->findByid($question_id);
						$data .= $question['Question']['id']."---".$question['Question']['question']."^^^";
						$answers = $this->Answer->findAllByquestion_id($question_id);
						$correct_id = 0;
						foreach($answers as $row)
						{
							$data .= $row['Answer']['id']."---".$row['Answer']['answer_text']."---".$row['Answer']['correct']."-*-";
						}
						$data = substr($data, 0, -3);
						$data .= "$%$";
					}
					$data = substr($data, 0, -3);
					$data .= "%%%";
				}
			}
			//trim the end of the qgroupData;
			$data = substr($data, 0, -3);
			$this->set('data', gzcompress($data, 8));
			//$this->set('data', $data);
		}
	}
	function assign_startpoint($student_id = null, $assessment_list = null , $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code') || $student_id == null || $assessment_list == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$assessments = explode("*", $assessment_list);
			$assessment_id = 0;
			for($i = 0; $i < count($assessments); ++$i)
			{
				//check if assessment is free
				$results = $this->Assessment->query("SELECT * FROM assigned_assessments WHERE assessment_id=".$assessments[$i]);
				if($results == null)
				{
					$this->Assessment->query("INSERT INTO assigned_assessments (student_number, assessment_id, course_id) VALUES ('".$student_id."','".$assessments[$i]."','4')");
					$assessment_id = $assessments[$i];
					break;
				}
			}

			
			if($assessment_id == 0)
				$this->set('data', gzcompress("unavailable", 8));
			else
				$this->set('data', gzcompress($assessment_id, 8));
			//$this->set('data', $assessment_id);
		}
	}
	function free_assessment($student_id = null, $assessment_id = null , $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code') || $assessment_id == null || $student_id == null)
		{
			$this->set('data', "Error");
		}
		else
		{
			$this->Assessment->query("DELETE FROM assigned_assessments WHERE assessment_id=".$assessment_id." AND student_number='".$student_id."'");
		}
	}
	function free_all($course_id = null, $pass = null)
	{
		if($pass != Configure::read('Settings.data_pass_code'))
		{
			$this->set('data', "Error");
		}
		else
		{
			$this->Assessment->query("TRUNCATE TABLE `assigned_assessments`");
			$this->set('data', "Done");
		}
	}
}
?>